package com.qingfeng.medical.controller.admin.hosp;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.biz.hosp.domain.vo.ScheduleDepPatientVo;
import com.qingfeng.medical.biz.hosp.domain.vo.ScheduleVo;
import com.qingfeng.medical.biz.hosp.domain.vo.ScheduleWeekDayVo;
import com.qingfeng.medical.biz.hosp.service.ScheduleService;
import com.qingfeng.medical.biz.information.domain.vo.DoctorVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;


/**
 * 排班管理
 *
 * @author 清风学Java
 * @date 2023-04-14 14:47:17
 */
@Api(tags = "排班管理管理")
@RestController
@RequestMapping("/admin/hosp/schedule")
@Validated
public class ScheduleController extends BaseController {

    @Autowired
    private ScheduleService scheduleService;

    @PreAuthorize("hasAuthority('bnt.schedule.deptree')")
    @ApiOperation(value = "查询科室门诊关系", notes = "查询科室门诊关系")
    @GetMapping("/dep/patient")
    @Log(title = "排班管理", businessType = BusinessType.SELECT, message = "查询科室门诊关系")
    public Result<List<ScheduleDepPatientVo>> findDepPatient() {
        List<ScheduleDepPatientVo> scheduleDepPatientList = scheduleService.findDepPatient();
        return Result.ok(scheduleDepPatientList);
    }

    @PreAuthorize("hasAuthority('bnt.schedule.wekday')")
    @ApiOperation(value = "分页查询排班日期数据", notes = "分页查询排班日期数据")
    @GetMapping("/weekday")
    @Log(title = "排班管理", businessType = BusinessType.SELECT, message = "分页查询排班日期数据")
    public Result<ScheduleWeekDayVo> scheduleWeekDay(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @RequestParam("page") @NotNull(message = "页码不能为空") Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @RequestParam("limit") @NotNull(message = "每页条目数不能为空") Long limit,
            @ApiParam(name = "depId", value = "科室门诊Id", required = true)
            @RequestParam("depId") @NotNull(message = "科室门诊Id不能为空") Long depId) {
        ScheduleWeekDayVo scheduleWeekDayVo = scheduleService.scheduleWeekDay(page, limit, depId);
        return Result.ok(scheduleWeekDayVo);
    }

    @PreAuthorize("hasAuthority('bnt.schedule.list')")
    @ApiOperation(value = "排班管理列表", notes = "排班管理列表")
    @GetMapping("/list")
    @Log(title = "排班管理", businessType = BusinessType.SELECT, message = "排班管理列表")
    public Result<List<ScheduleVo>> scheduleList(
            @ApiParam(name = "weekDay", value = "排班日期", required = true)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
            @RequestParam("weekDay") Date weekDay,
            @ApiParam(name = "depId", value = "科室门诊Id", required = true)
            @RequestParam("depId") Long depId) {
        List<ScheduleVo> scheduleVo = scheduleService.findScheduleVo(weekDay, depId);
        return Result.ok(scheduleVo);
    }

    @PreAuthorize("hasAuthority('bnt.schedule.add')")
    @ApiOperation(value = "查询科室门诊下是否有空余的医生需要新增排班", notes = "查询科室门诊下是否有空余的医生需要新增排班")
    @GetMapping("/free/doctor")
    @Log(title = "排班管理", businessType = BusinessType.SELECT, message = "查询科室门诊下是否有空余的医生需要新增排班")
    public Result<List<DoctorVo>> getFreeDoctorList(
            @ApiParam(name = "weekDay", value = "排班日期", required = true)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
            @RequestParam("weekDay") @NotNull(message = "排班日期不能为空") Date weekDay,
            @ApiParam(name = "depId", value = "科室门诊Id", required = true)
            @RequestParam("depId") @NotNull(message = "科室-门诊Id不能为空") Long depId) {
        List<DoctorVo> doctorVoList = scheduleService.findFreeDoctorList(weekDay, depId);
        return Result.ok(doctorVoList);
    }

    @Deprecated
    @ApiOperation(value = "门诊停诊，那个门诊停诊，当天的排班全部修改状态为禁用，并发布停诊通知", notes = "门诊停诊，那个门诊停诊，当天的排班全部修改状态为禁用，并发布停诊通知")
    @PutMapping("/cease")
    @Log(title = "排班管理", businessType = BusinessType.UPDATE, message = "门诊停诊，那个门诊停诊，当天的排班全部修改状态为禁用，并发布停诊通知")
    public Result ceaseSchedule(
            @ApiParam(name = "weekDay", value = "排班日期", required = true)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
            @RequestParam("weekDay") @NotNull(message = "排班日期不能为空") Date weekDay,
            @ApiParam(name = "depId", value = "科室门诊Id", required = true)
            @RequestParam("depId") @NotNull(message = "科室-门诊Id不能为空") Long depId){
        // 那个科室-门诊，那一天的需要停止
        scheduleService.ceaseSchedule(weekDay, depId);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.schedule.add')")
    @ApiOperation(value = "一键排班", notes = "一键排班")
    @PostMapping("/save")
    @Log(title = "排班管理", businessType = BusinessType.INSERT, message = "保存排班管理信息")
    public Result save(
            @ApiParam(name = "userId", value = "用户Id", required = true)
            @RequestParam("userId") @NotNull(message = "用户Id不能为空") Long userId,
            @ApiParam(name = "weekDay", value = "排班日期", required = true)
            @DateTimeFormat(pattern = "yyyy-MM-dd")
            @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
            @RequestParam("weekDay") @NotNull(message = "排班日期不能为空") Date weekDay,
            @ApiParam(name = "depId", value = "科室门诊Id", required = true)
            @RequestParam("depId") @NotNull(message = "科室-门诊Id不能为空") Long depId) {
        scheduleService.saveSchedule(userId, weekDay, depId);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.schedule.update')")
    @ApiOperation(value = "修改排班状态", notes = "修改排班状态")
    @PutMapping("/status")
    @Log(title = "排班管理", businessType = BusinessType.UPDATE, message = "修改排班状态")
    public Result updateStatus(@RequestParam("id") Long id) {
        scheduleService.updateStatusById(id);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.schedule.delete')")
    @ApiOperation(value = "删除排班管理信息", notes = "删除排班管理信息")
    @DeleteMapping("/delete")
    @Log(title = "排班管理", businessType = BusinessType.DELETE, message = "删除排班管理信息")
    public Result delete(@RequestParam("id") Long id) {
        scheduleService.removeById(id);
        return Result.ok();
    }

    // TODO 医师个人的信息查询

}
